<?php
final class TestModelController
    extends Lib_Default_ActionAbstract
{

    public final function init()
    {
        parent::init();
        ini_set('max_execution_time', 600);
        ini_set('memory_limit', "250M");
    }

//    /**
//     * Create 10000 test users records
//     */
//    public final function insertAction()
//    {
//        $testData = array(
//            'password' => '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', // 'password' in sha1
//        );
//
//        /* @var $usersModel Table_Users */
//        $userModel = AdamP_Model::getTable('Users');
//        $db = CL_Model_Connection::getConnection();
//        for ($i = 0; $i < 10000; $i++) {
//            $testData['login'] = 'test_user0' . $i;
//            $db->insert('users', $testData);
//        }
//
//        $this->_finish();
//    }

    /**
     * Create 10000 test users records
     */
    public final function saveAction()
    {
        AdamP_Debug_Firebug::logCurrentExecutionTime();
        $testData = array(
            'password' => '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', // 'password' in sha1
        );

        $conn = AdamP_Model_Connection::getConnection();
        for ($i = 0; $i < 10000; $i++) {
            $testData['login'] = 'test_user0' . $i;
            //$conn->insert('users', $testData);
            $conn->query("insert into users (`login`, `password`) values('" . $testData['login'] . "', '" . $testData['password'] . "')");
        }

        AdamP_Debug_Firebug::logCurrentExecutionTime();
        AdamP_Debug_Firebug::writeOutput();

        $this->_finish();
    }

    /**
     * Update all users records
     * (add 'hej' prefix to all logins ;))
     */
    public final function updateAction()
    {
        /* @var $usersModel Table_Users */
        $userModel = AdamP_Model::getTable('Users');
        $usersData = $userModel->findAll();
        foreach ($usersData as $data) {
            $data['login'] .= 'hej';
            $userModel->save($data);
        }

        $this->_finish();
    }

    /**
     * Delete all users records
     */
    public final function deleteAction()
    {
        /* @var $usersModel Table_Users */
        $userModel = AdamP_Model::getTable('Users');
        $usersData = $userModel->findAll();
        foreach ($usersData as $data) {
            $userModel->delete('id = ' . (int)$data['id']);
        }

        $this->_finish();
    }

    private function _finish()
    {
        exit();
    }
}